#include "base/Bucket.h"
#include "util/Logger.h"

NEW_DELETE_OPERATORS_CPP(Bucket,1000);

Bucket::Bucket (const char* _name) {
    outputName = _name;
    rollbackEnabled = false;
    value = 0;
    specValue = 0;
    voted = false;
}

Bucket::Bucket (const char* _name, Bucket* _rollbackRecipient) {
    outputName = _name;
    rollbackEnabled = true;
    recipient = _rollbackRecipient;
    value = 0;
    specValue = 0;
    voted = false;
}

Bucket::~Bucket() {}

void Bucket::vote () {
    voted = true;
}

void Bucket::unvote () {
    voted = false;
}

bool Bucket::getVoteStatus () {
    return voted;
}

const char* Bucket::getName () {
    return outputName;
}

void Bucket::elect (bool transactional) {
    if (rollbackEnabled && transactional) {
        specValue++;
    } else {
        value++;
    }
}

void Bucket::commit () {
    ASSERT(rollbackEnabled);
    value += specValue;
    specValue = 0;
}

void Bucket::rollback () {
    ASSERT(rollbackEnabled);
    
    recipient->value += specValue;
    specValue = 0;
}
